package Services.Impl;

import Constraints.IConstraint;
import Exceptions.ConstraintErrorException;
import Model.Core.IElement;
import Model.Draw.Events.DrawCompleteListener;
import Model.Draw.Events.DrawEvent;
import Model.Draw.Events.ShowTipListener;
import Services.IDrawService;
import Workflow.IWorkflow;
import Workflow.WorkflowFactory;

/**
 * TCC Project
 * @author José Luz  e Michele Puretachi
 */
public class DrawService implements IDrawService, DrawCompleteListener {

    private IWorkflow workflow;
    private static boolean enableWorkflow;
    
    public DrawService() {
        workflow = WorkflowFactory.startWorkflow();
        workflow.registerDrawCompleteListener(this);
        enableWorkflow = false;
    }
    
    @Override
    public void registerShowTipsListener(ShowTipListener listener){
        workflow.registerShowTipsListener(listener);
    }
    
    @Override
    public void configureContraint(IConstraint constraint) {
        workflow.setWorkflowValidation(constraint);
    }

    @Override
    public void newEntry(Object item) throws ConstraintErrorException {
        if (enableWorkflow){
            workflow.setItem(item);
        }
    }

    @Override
    public void completeDrawHandler(DrawEvent e) {
        WorkflowFactory.restoreWorkflow(workflow);
        ((IElement) e.getSource()).manifestGraphically(e.getOriginElement(), e.getTargetElement());
        enableWorkflow = false;
    }

    @Override
    public void setCurrentElement(IElement element) {
        WorkflowFactory.restoreWorkflow(workflow);
        workflow.setElement(element);
        enableWorkflow = true;
    }
}